package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.common.dto.ListResult;
import com.mysoft.gfyx.marketdevelop.mip.GetProjectDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.GetProjectDetailTableRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiCommonService;
import com.mysoft.gfyx.marketdevelop.service.ProjectDetailTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

@Service
public class ProjectDetailTableServiceImp extends AppService implements ProjectDetailTableService {
    @Autowired
    private GetProjectDataService getProjectDataService;

    @Autowired
    private LoadDataApiCommonService loadDataApiCommonService;

    public FieldsSetting loadFieldsProjectAboutCompany() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_about_company";
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("corp_name", groupName, "企业名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("corprole_num", groupName, "企业角色", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("organizing_code", groupName, "企业统一信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("person_name", groupName, "负责人姓名", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("person_phone", groupName, "负责人电话", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectAboutCompany(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectAboutCompany(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {

        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    public FieldsSetting loadFieldsProjectContract() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_contract";
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contractor_corp_name", groupName, "承包单位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("proprietor_corp_name", groupName, "发包单位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contract_date", groupName, "签订日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contract_money", groupName, "合同金额(万元)", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contract_type_num", groupName, "合同类型", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectContract(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectContract(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            if (p.get("contract_date") != null)
                p.put("contract_date", p.get("contract_date").toString().substring(0, 10));
        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    public FieldsSetting loadFieldsProjectBidding() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_bidding";
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("tender_corp_name", groupName, "中标单位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("tender_result_date", groupName, "中标日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("tender_money", groupName, "中标金额(万元)", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("constructor_name", groupName, "项目负责人", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("tender_class_num", groupName, "招标类型", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectBidding(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectBidding(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            if (p.get("tender_result_date") != null)
                p.put("tender_result_date", p.get("tender_result_date").toString().substring(0, 10));
        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    public FieldsSetting loadFieldsProjectConstruction() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_construction";
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("censor_corp_name", groupName, "施工图审查机构", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("censor_e_date", groupName, "审查完成时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("one_censor_is_pass", groupName, "一次审查是否通过", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("one_censor_wfqt_count", groupName, "一次审查时违反强制性标准数", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectConstruction(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectConstruction(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            if (p.get("censor_e_date") != null)
                p.put("censor_e_date", p.get("censor_e_date").toString().substring(0, 10));

            if (p.get("one_censor_is_pass") != null && !p.get("one_censor_is_pass").equals("")) {
                if (p.get("one_censor_is_pass").equals(1)) {
                    p.put("one_censor_is_pass", "是");
                } else {
                    p.put("one_censor_is_pass", "否");
                }
            }
        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    public FieldsSetting loadFieldsProjectLicense() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_license";
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("release_date", groupName, "发证时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contract_money", groupName, "合同金额(万元)", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("area", groupName, "面积(平方米)", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("construction_corp_name", groupName, "施工企业", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("supervisor_corp_name", groupName, "监理企业", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectLicense(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectLicense(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            if (p.get("release_date") != null)
                p.put("release_date", p.get("release_date").toString().substring(0, 10));
        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }


    public FieldsSetting loadFieldsProjectCompletionRecord() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_completion_record";
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("b_date", groupName, "实际开工日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("fact_cost", groupName, "实际造价(万元)", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("e_date", groupName, "竣工验收备案日期", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("fact_size", groupName, "实际建设规模", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectCompletionRecord(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectCompletionRecord(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            if (p.get("b_date") != null)
                p.put("b_date", p.get("b_date").toString().substring(0, 10));

            if (p.get("e_date") != null)
                p.put("e_date", p.get("e_date").toString().substring(0, 10));
        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    public FieldsSetting loadFieldsProjectSupplierContact() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_supplier_contact";
        fieldsSetting.getFields().add(new DataApiField("id", groupName, "主键ID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("supplier_name", groupName, "企业名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_user_name", groupName, "联系人", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_phone", groupName, "联系电话", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_create_time", groupName, "更新时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_source", groupName, "来源", FieldType.String));
        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectSupplierContact(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectSupplierContact(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            String phone = p.get("contact_phone").toString();
            if (phone.length() > 7) {
                p.put("contact_phone", phone.substring(0, 3) + "****" + phone.substring(7));
            }
        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    public FieldsSetting loadFieldsProjectDeveloperContact() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "project_developer_contact";
        fieldsSetting.getFields().add(new DataApiField("id", groupName, "主键ID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("prj_num", groupName, "项目编码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("sub_developer_name", groupName, "企业名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_user_name", groupName, "联系人", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_phone", groupName, "联系电话", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_create_time", groupName, "更新时间", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("contact_source", groupName, "来源", FieldType.String));
        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadDataProjectDeveloperContact(LoadDataParams options) {
        if (options.getOptions().getContext().get("id") == null || options.getOptions().getContext().get("id").toString().equals("")) {
            return new ListDataResult();
        }
        GetProjectDetailTableRequestDto dto = new GetProjectDetailTableRequestDto();
        dto.setBase_uid(options.getOptions().getContext().get("id").toString());
        dto.setPageSize(options.getPageSize());
        dto.setPageNum(options.getPageIndex() + 1);
        dto.addFilterByLoadDataApi(options.getOptions().getFilters());

        SqlApiResponseDto res = getProjectDataService.getProjectDeveloperContact(dto);
        if (res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        res.getData().getRows().forEach(p -> {
            String phone = p.get("contact_phone").toString();
            if (phone.length() > 7) {
                p.put("contact_phone", phone.substring(0, 3) + "****" + phone.substring(7));
            }
        });
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

}
